<!DOCTYPE html>
<html  lang="zh">
<head>
    <meta charset="utf-8" />

<meta name="generator" content="Hexo 3.8.0" />

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />

<title>iOS 代码规范 - OBJC.VIP</title>


    <meta name="description" content="iOS 开发的代码规范。">
<meta name="keywords" content="代码规范">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS 代码规范">
<meta property="og:url" content="https://objcvip.github.io/CodingGuidelines/index.html">
<meta property="og:site_name" content="OBJC.VIP">
<meta property="og:description" content="iOS 开发的代码规范。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://objcvip.github.io/images/og_image.png">
<meta property="og:updated_time" content="2019-09-08T09:10:08.791Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="iOS 代码规范">
<meta name="twitter:description" content="iOS 开发的代码规范。">
<meta name="twitter:image" content="https://objcvip.github.io/images/og_image.png">







<link rel="icon" href="/images/favicon.svg">


<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.7.2/css/bulma.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Ubuntu:400,600|Source+Code+Pro">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@9.12.0/styles/atom-one-dark.css">


    
    
    
    <style>body>.footer,body>.navbar,body>.section{opacity:0}</style>
    

    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/css/lightgallery.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/css/justifiedGallery.min.css">
    

    
    

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.css">


    
    
    
    

<link rel="stylesheet" href="/css/back-to-top.css">


    
    

    
    
    
    

    
    
<link rel="stylesheet" href="/css/progressbar.css">
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

    
    
    


<link rel="stylesheet" href="/css/style.css">
</head>
<body class="is-1-column">
    <nav class="navbar navbar-main">
    <div class="container">
        <div class="navbar-brand is-flex-center">
            <a class="navbar-item navbar-logo" href="/">
            
                <img src="/images/logo.svg" alt="iOS 代码规范" height="28">
            
            </a>
        </div>
        <div class="navbar-menu">
            
            <div class="navbar-start">
                
                <a class="navbar-item"
                href="/">首页</a>
                
                <a class="navbar-item"
                href="/archives">归档</a>
                
                <a class="navbar-item"
                href="/categories">分类</a>
                
                <a class="navbar-item"
                href="/tags">标签</a>
                
                <a class="navbar-item"
                href="/links">友链</a>
                
                <a class="navbar-item"
                href="/about">关于</a>
                
            </div>
            
            <div class="navbar-end">
                
                
                
                <a class="navbar-item search" title="搜索" href="javascript:;">
                    <i class="fas fa-search"></i>
                </a>
                
            </div>
        </div>
    </div>
</nav>
    
    <section class="section">
        <div class="container">
            <div class="columns">
                <div class="column is-12 has-order-2 column-main"><div class="card">
    
    <div class="card-content article ">
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2019-03-04T16:00:00.000Z">2019-03-05</time>
                
                <div class="level-item">
                <a class="has-link-grey -link" href="/categories/代码规范/">代码规范</a>
                </div>
                
                
                <span class="level-item has-text-grey">
                    
                    
                    17 分钟 读完 (大约 2479 个字)
                </span>
                
                
            </div>
        </div>
        
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                iOS 代码规范
            
        </h1>
        <div class="content">
            <p>iOS 开发的代码规范。<br><a id="more"></a><br>原文链接：<a href="https://juejin.im/post/5c7c7e0cf265da2ddb298123" target="_blank" rel="noopener">有了这些你们团队的代码肯定规范</a></p>
<h1 id="原则"><a href="#原则" class="headerlink" title="原则"></a>原则</h1><ol>
<li>长的、描述性的方法和变量命名是好的命名方式。不要使用简写，除非是一些大家都知道的场景比如 VIP。不要使用 bgView，推荐使用 backgroundView。</li>
<li>见名知意。含义清楚，做好不加注释代码自我表述能力强。（前提是代码足够规范）</li>
<li>不要过分追求技巧，降低代码可读性。</li>
<li>删除没必要的代码。比如我们新建一个控制器，里面会有一些不会用到的代码，或者注释起来的代码，如果这些代码不需要，那就删除它，留着偷懒吗？下次需要自己手写。</li>
<li>在方法内部不要重复计算某个值，适当的情况下可以将计算结果缓存起来。</li>
<li>尽量减少单例的使用。</li>
<li>提供一个统一的数据管理入口，不管是 MVC、MVVM、MVP 模块内提供一个统一的数据管理入口会使得代码变得更容易管理和维护。</li>
<li>除了 .m 文件中方法，其他的地方”{“不需要另起一行。</li>
</ol>
<figure class="highlight objc hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="hljs-keyword">void</span>)getGooodsList</span><br><span class="line">&#123;</span><br><span class="line">    <span class="hljs-comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="hljs-keyword">void</span>)doHomework</span><br><span class="line">&#123;</span><br><span class="line">    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">self</span>.hungry) &#123;</span><br><span class="line">        <span class="hljs-keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">self</span>.thirsty) &#123;</span><br><span class="line">        <span class="hljs-keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">self</span>.tired) &#123;</span><br><span class="line">        <span class="hljs-keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    papapa.then.over;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h1><ol>
<li>一个变量最好只有一个作用，切勿为了节省代码行数，觉得一个变量可以做多个用途。（单一原则）</li>
<li>方法内部如果有局部变量，那么局部变量应该靠近在使用的地方，而不是全部在顶部声明全部的局部变量。</li>
</ol>
<h1 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h1><ol>
<li>1元运算符和变量之间不需要空格。例如：<code>++n</code></li>
<li>2元运算符与变量之间需要空格隔开。例如： <code>containerWidth = 0.3 * Screen_Width</code><br>当有多个运算符的时候需要使用括号来明确正确的顺序，可读性较好。例如：<code>2 &lt;&lt; (1 + 2 * 3 - 4)</code></li>
</ol>
<h1 id="条件表达式"><a href="#条件表达式" class="headerlink" title="条件表达式"></a>条件表达式</h1><ol>
<li><p>当有条件过多、过长的时候需要换行，为了代码看起来整齐些。</p>
<figure class="highlight objc hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-comment">//good</span></span><br><span class="line"><span class="hljs-keyword">if</span> (condition1() &amp;&amp; </span><br><span class="line">    condition2() &amp;&amp; </span><br><span class="line">    condition3() &amp;&amp; </span><br><span class="line">    condition4()) &#123;</span><br><span class="line">  <span class="hljs-comment">// Do something</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="hljs-comment">//bad</span></span><br><span class="line"><span class="hljs-keyword">if</span> (condition1() &amp;&amp; condition2() &amp;&amp; condition3() &amp;&amp; condition4(）) &#123; <span class="hljs-comment">// Do something &#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>在一个代码块里面有个可能的情况时善于使用 return 来结束异常的情况。</p>
<figure class="highlight objc hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="hljs-keyword">void</span>)doHomework</span><br><span class="line">&#123;</span><br><span class="line">    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">self</span>.hungry) &#123;</span><br><span class="line">        <span class="hljs-keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">self</span>.thirsty) &#123;</span><br><span class="line">        <span class="hljs-keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">self</span>.tired) &#123;</span><br><span class="line">        <span class="hljs-keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    papapa.then.over;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>每个分支的实现都必须使用 {} 包含。</p>
<figure class="highlight objc hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-comment">// bad</span></span><br><span class="line"><span class="hljs-keyword">if</span> (<span class="hljs-keyword">self</span>.hungry) <span class="hljs-keyword">self</span>.eat() </span><br><span class="line"><span class="hljs-comment">// good</span></span><br><span class="line"><span class="hljs-keyword">if</span> (<span class="hljs-keyword">self</span>.hungry) &#123;</span><br><span class="line">    <span class="hljs-keyword">self</span>.eat()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>条件判断的时候应该是变量在左，条件在右。 if ( currentCursor == 2 ) { //… }</p>
</li>
<li>switch 语句后面的每个分支都需要用大括号括起来。</li>
<li>switch 语句后面的 default 分支必须存在，除非是在对枚举进行 switch。<figure class="highlight objc hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-keyword">switch</span> (menuType) &#123;  </span><br><span class="line">  <span class="hljs-keyword">case</span> menuTypeLeft: &#123;</span><br><span class="line">    <span class="hljs-comment">// ...  </span></span><br><span class="line">    <span class="hljs-keyword">break</span>; </span><br><span class="line">   &#125;</span><br><span class="line">  <span class="hljs-keyword">case</span> menuTypeRight: &#123;</span><br><span class="line">    <span class="hljs-comment">// ...  </span></span><br><span class="line">    <span class="hljs-keyword">break</span>; </span><br><span class="line">  &#125;</span><br><span class="line">  <span class="hljs-keyword">case</span> menuTypeTop: &#123;</span><br><span class="line">    <span class="hljs-comment">// ...  </span></span><br><span class="line">    <span class="hljs-keyword">break</span>; </span><br><span class="line">  &#125;</span><br><span class="line">  <span class="hljs-keyword">case</span> menuTypeBottom: &#123;</span><br><span class="line">    <span class="hljs-comment">// ...  </span></span><br><span class="line">    <span class="hljs-keyword">break</span>; </span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h1 id="类名"><a href="#类名" class="headerlink" title="类名"></a>类名</h1><ol>
<li>大写驼峰式命名。每个单词首字母大写。比如「申请记录控制器」ApplyRecordsViewController</li>
<li>每个类型的命名以该类型结尾。</li>
</ol>
<ul>
<li>ViewController：使用 ViewController 结尾。例子：ApplyRecordsViewController</li>
<li>View：使用 View 结尾。例子：分界线：boundaryView</li>
<li>NSArray：使用 s 结尾。比如商品分类数据源。categories</li>
<li>UITableViewCell：使用 Cell 结尾。比如 MyProfileCell</li>
<li>Protocol：使用 Delegate 或者 Datasource 结尾。比如 XQScanViewDelegate</li>
<li>Tool：工具类</li>
<li>代理类：Delegate</li>
<li>Service 类：Service</li>
</ul>
<h1 id="类的注释"><a href="#类的注释" class="headerlink" title="类的注释"></a>类的注释</h1><p>有时候我们需要为我们创建的类设置一些注释。我们可以在类的下面添加。</p>
<h1 id="枚举"><a href="#枚举" class="headerlink" title="枚举"></a>枚举</h1><p>枚举的命名和类的命名相近。<br><figure class="highlight objc hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-keyword">typedef</span> <span class="hljs-built_in">NS_ENUM</span>(<span class="hljs-built_in">NSInteger</span>, <span class="hljs-built_in">UIControlContentVerticalAlignment</span>) &#123;</span><br><span class="line">    <span class="hljs-built_in">UIControlContentVerticalAlignmentCenter</span>  = <span class="hljs-number">0</span>,</span><br><span class="line">    <span class="hljs-built_in">UIControlContentVerticalAlignmentTop</span>     = <span class="hljs-number">1</span>,</span><br><span class="line">    <span class="hljs-built_in">UIControlContentVerticalAlignmentBottom</span>  = <span class="hljs-number">2</span>,</span><br><span class="line">    <span class="hljs-built_in">UIControlContentVerticalAlignmentFill</span>    = <span class="hljs-number">3</span>,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h1 id="宏"><a href="#宏" class="headerlink" title="宏"></a>宏</h1><ol>
<li>全部大写，单词与单词之间用 _ 连接。</li>
<li>以 K 开头。后面遵循大写驼峰命名。「不带参数」<figure class="highlight objc hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">#define HOME_PAGE_DID_SCROLL @<span class="hljs-meta-string">"com.xq.home.page.tableview.did.scroll"</span></span></span><br><span class="line"><span class="hljs-meta">#define KHomePageDidScroll @<span class="hljs-meta-string">"com.xq.home.page.tableview.did.scroll"</span></span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<h1 id="属性"><a href="#属性" class="headerlink" title="属性"></a>属性</h1><p>书写规则，基本上就是 @property 之后空一格，括号，里面的 线程修饰词、内存修饰词、读写修饰词，空一格 类 对象名称 根据不同的场景选择合适的修饰符。<br><figure class="highlight objc hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-keyword">@property</span> (<span class="hljs-keyword">nonatomic</span>, <span class="hljs-keyword">strong</span>) <span class="hljs-built_in">UITableView</span> *tableView;</span><br><span class="line"><span class="hljs-keyword">@property</span> (<span class="hljs-keyword">nonatomic</span>, <span class="hljs-keyword">assign</span>, <span class="hljs-keyword">readonly</span>) <span class="hljs-built_in">BOOL</span> loading;   </span><br><span class="line"><span class="hljs-keyword">@property</span> (<span class="hljs-keyword">nonatomic</span>, <span class="hljs-keyword">weak</span>) <span class="hljs-keyword">id</span>&lt;<span class="hljs-meta">#delegate#&gt; delegate;</span></span><br><span class="line"><span class="hljs-keyword">@property</span> (<span class="hljs-keyword">nonatomic</span>, <span class="hljs-keyword">copy</span>) &lt;<span class="hljs-meta">#returnType#&gt; (^<span class="hljs-meta-string">&lt;#Block#&gt;</span>)(<span class="hljs-meta-string">&lt;#parType#&gt;</span>);</span></span><br></pre></td></tr></table></figure></p>
<h1 id="单例"><a href="#单例" class="headerlink" title="单例"></a>单例</h1><p>单例适合全局管理状态或者事件的场景。一旦创建，对象的指针保存在静态区，单例对象在堆内存中分配的内存空间只有程序销毁的时候才会释放。基于这种特点，那么我们类似 UIApplication 对象，需要全局访问唯一一个对象的情况才适合单例，或者访问频次较高的情况。我们的功能模块的生命周期肯定小于 App 的生命周期，如果多个单例对象的话，势必 App 的开销会很大，糟糕的情况系统会杀死 App。如果觉得非要用单例比较好，那么注意需要在合适的场合 tearDown 掉。</p>
<p>单例的使用场景概括如下：</p>
<ul>
<li>控制资源的使用，通过线程同步来控制资源的并发访问。</li>
<li>控制实例的产生，以达到节约资源的目的。</li>
<li>控制数据的共享，在不建立直接关联的条件下，让多个不相关的进程或线程之间实现通信。</li>
</ul>
<h1 id="私有变量"><a href="#私有变量" class="headerlink" title="私有变量"></a>私有变量</h1><p>推荐以<code>_</code>开头，写在 .m 文件中。例如<code>NSString * _somePrivateVariable</code></p>
<h2 id="代理方法"><a href="#代理方法" class="headerlink" title="代理方法"></a>代理方法</h2><ol>
<li>类的实例必须作为方法的参数之一。</li>
<li>对于一些连续的状态的，可以加一些 will（将要）、did（已经）</li>
<li>以类的名称开头<figure class="highlight objc hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="hljs-keyword">void</span>)tableView:(<span class="hljs-built_in">UITableView</span> *)tableView willDisplayCell:(<span class="hljs-built_in">UITableViewCell</span> *)cell forRowAtIndexPath:(<span class="hljs-built_in">NSIndexPath</span> *)indexPath;</span><br><span class="line"></span><br><span class="line">- (<span class="hljs-keyword">void</span>)tableView:(<span class="hljs-built_in">UITableView</span> *)tableView didEndDisplayingCell:(<span class="hljs-built_in">UITableViewCell</span> *)cell forRowAtIndexPath:(<span class="hljs-built_in">NSIndexPath</span> *)indexPath;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h2 id="方法"><a href="#方法" class="headerlink" title="方法"></a>方法</h2><ol>
<li>方法与方法之间间隔一行</li>
<li>大量的方法尽量要以组的形式放在一起，比如生命周期函数、公有方法、私有方法、setter &amp;&amp; getter、代理方法..</li>
<li>方法最后面的括号需要另起一行。遵循 Apple 的规范</li>
<li>对于其他场景的括号，括号不需要单独换行。比如 if 后面的括号。</li>
<li>如果方法参数过多过长，建议多行书写。用冒号进行对齐。</li>
<li>一个方法内的代码最好保持在50行以内，一般经验来看如果一个方法里面的代码行数过多，代码的阅读体验就很差（别问为什么，做过重构代码行数很长的人都有类似的心情）</li>
<li>一个函数只做一个事情，做到单一原则。所有的类、方法设计好后就可以类似搭积木一样实现一个系统。</li>
<li>对于有返回值的函数，且函数内有分支情况。确保每个分支都有返回值。</li>
<li>函数如果有多个参数，外部传入的参数需要检验参数的非空、数据类型的合法性，参数错误做一些措施：立即返回、断言。</li>
<li>多个函数如果有逻辑重复的代码，建议将重复的部分抽取出来，成为独立的函数进行调用</li>
<li><p>方法如果有多个参数的情况下需要注意是否需要介词和连词。很多时候在不知道如何抉择测时候思考下苹果的一些 API 的方法命名。</p>
<figure class="highlight objc hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-comment">//good</span></span><br><span class="line">- (<span class="hljs-keyword">instancetype</span>)initWithAge:(<span class="hljs-built_in">NSInteger</span>)age name:(<span class="hljs-built_in">NSString</span> *)name;</span><br><span class="line"></span><br><span class="line">- (<span class="hljs-keyword">void</span>)tableView:(<span class="hljs-built_in">UITableView</span> *)tableView didSelectRowAtIndexPath:(<span class="hljs-built_in">NSIndexPath</span> *)indexPath;</span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">//bad</span></span><br><span class="line">- (<span class="hljs-keyword">instancetype</span>)initWithAge:(<span class="hljs-built_in">NSInteger</span>)age andName:(<span class="hljs-built_in">NSString</span> *)name;</span><br><span class="line"></span><br><span class="line">- (<span class="hljs-keyword">void</span>)tableView:(<span class="hljs-built_in">UITableView</span> *)tableView :(<span class="hljs-built_in">NSIndexPath</span> *)indexPath;</span><br></pre></td></tr></table></figure>
</li>
<li><p>.m 文件中的私有方法需要在顶部进行声明 </p>
</li>
<li>方法组之间也有个顺序问题。</li>
</ol>
<ul>
<li>在文件最顶部实现属性的声明、私有方法的声明（很多人省去这一步，问题不大，但是蛮多第三方的库都写了，看起来还是会很方便，建议书写）。</li>
<li>在生命周期的方法里面，比如 viewDidLoad 里面只做界面的添加，而不是做界面的初始化，所有的 view 初始化建议放在 getter 里面去做。往往 view 的初始化的代码长度会比较长、且一般会有多个 view 所以 getter 和 setter 一般建议放在最下面，这样子顶部就可以很清楚的看到代码的主要逻辑。</li>
<li>所有button、gestureRecognizer 的响应事件都放在这个区域里面，不要到处乱放。</li>
</ul>
<p>文件基本上就是<br><figure class="highlight objc hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">#import <span class="hljs-meta-string">"ViewController.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">/*ViewController*/</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">/*View&amp;&amp;Util*/</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">/*model*/</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">/*NetWork InterFace*/</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">/*Vender*/</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-class"><span class="hljs-keyword">@interface</span> <span class="hljs-title">ViewController</span> ()</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-keyword">@end</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-class"><span class="hljs-keyword">@implementation</span> <span class="hljs-title">ViewController</span></span></span><br><span class="line"></span><br><span class="line"><span class="hljs-meta">#pragma mark - life cycle</span></span><br><span class="line"></span><br><span class="line">- (<span class="hljs-keyword">void</span>)viewWillAppear:(<span class="hljs-built_in">BOOL</span>)animated</span><br><span class="line">&#123;</span><br><span class="line">[<span class="hljs-keyword">super</span> viewDidAppear:animated];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="hljs-keyword">void</span>)viewDidAppear:(<span class="hljs-built_in">BOOL</span>)animated</span><br><span class="line">&#123;</span><br><span class="line">[<span class="hljs-keyword">super</span> viewDidAppear:animated];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="hljs-keyword">void</span>)viewDidLoad</span><br><span class="line">&#123;</span><br><span class="line">[<span class="hljs-keyword">super</span> viewDidLoad];</span><br><span class="line"><span class="hljs-keyword">self</span>.title = <span class="hljs-string">@"标准模版"</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="hljs-keyword">void</span>)viewWillDisappear:(<span class="hljs-built_in">BOOL</span>)animated</span><br><span class="line">&#123;</span><br><span class="line">[<span class="hljs-keyword">super</span> viewDidAppear:animated];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="hljs-keyword">void</span>)viewDidDisappear:(<span class="hljs-built_in">BOOL</span>)animated</span><br><span class="line">&#123;</span><br><span class="line">[<span class="hljs-keyword">super</span> viewDidAppear:animated];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="hljs-keyword">void</span>)dealloc</span><br><span class="line">&#123;</span><br><span class="line"><span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%s"</span>,__func__);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="hljs-meta">#pragma mark - public Method</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-meta">#pragma mark - private method</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-meta">#pragma mark - event response</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-meta">#pragma mark - UITableViewDelegate</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-meta">#pragma mark - UITableViewDataSource</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">//...(多个代理方法依次往下写)</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-meta">#pragma mark - getters and setters</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-keyword">@end</span></span><br></pre></td></tr></table></figure></p>
<h2 id="图片资源"><a href="#图片资源" class="headerlink" title="图片资源"></a>图片资源</h2><ol>
<li>单个文件的命名 文件资源的命名也需要一定的规范，形式为：<code>功能模块名_类别_功能_状态</code><br>@nx.png <a href="mailto:Setting_Button_search_selected@2x.png" target="_blank" rel="noopener">Setting_Button_search_selected@2x.png</a>、<a href="mailto:Setting_Button_search_selected@3x.png" target="_blank" rel="noopener">Setting_Button_search_selected@3x.png</a> <a href="mailto:Setting_Button_search_unselected@2x.png" target="_blank" rel="noopener">Setting_Button_search_unselected@2x.png</a>、<a href="mailto:Setting_Button_search_unselected@3x.png" target="_blank" rel="noopener">Setting_Button_search_unselected@3x.png</a></li>
<li>资源的文件夹命名 最好也参考 App 按照功能模块建立对应的实体文件夹目录，最后到对应的目录下添加相应的资源文件。</li>
</ol>
<h2 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h2><ol>
<li>对于类的注释写在当前类文件的顶部</li>
<li>对于属性的注释需要写在属性后面的地方。 <code>//**&lt;userId*/</code></li>
<li>对于 .h 文件中方法的注释，一律按快捷键 <code>command+option+/</code>。三个快捷键解决。按需在旁边对方法进行说明解释、返回值、参数的说明和解释 </li>
<li>对于 .m 文件中的方法的注释，在方法的旁边添加 //。 </li>
<li>注释符和注释内容需要间隔一个空格。 例如： // fetch goods list</li>
</ol>
<h2 id="版本规范"><a href="#版本规范" class="headerlink" title="版本规范"></a>版本规范</h2><p>采用 A.B.C 三位数字命名，比如：1.0.2，当有更新的情况下按照下面的依据</p>
<p>版本号示例<br>A.b.c属于重大内容的更新1.0.2 -&gt; 2.0.0<br>a.B.c属于小部分内容的更新1.0.2 -&gt; 1.1.1<br>a.b.C属于补丁更新1.0.2 -&gt; 1.0.3</p>

        </div>
        
        <div class="level is-size-7 is-uppercase">
            <div class="level-start">
                <div class="level-item">
                    <span class="is-size-6 has-text-grey has-mr-7">#</span>
                    <a class="has-link-grey -link" href="/tags/代码规范/">代码规范</a>
                </div>
            </div>
        </div>
        
        
        
    </div>
</div>





<div class="card card-transparent">
    <div class="level post-navigation is-flex-wrap is-mobile">
        
        <div class="level-start">
            <a class="level level-item has-link-grey  article-nav-prev" href="/DataStructurePart1/">
                <i class="level-item fas fa-chevron-left"></i>
                <span class="level-item">《大话数据结构》一</span>
            </a>
        </div>
        
        
        <div class="level-end">
            <a class="level level-item has-link-grey  article-nav-next" href="/用两个栈实现队列/">
                <span class="level-item">【算法】用两个栈实现队列</span>
                <i class="level-item fas fa-chevron-right"></i>
            </a>
        </div>
        
    </div>
</div>



<div class="card">
    <div class="card-content">
        <h3 class="title is-5 has-text-weight-normal">评论</h3>
        
<div id="comment-container"></div>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1.6.0/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1.6.0/dist/gitalk.min.js"></script>

<script>
    var gitalk = new Gitalk({
        clientID: '7cf9ae5a9ae4228f59ef',
        clientSecret: 'a60824892847d86dab15dae00b18a8227f9af769',
        id: '41711f2eb92eba3802e1056699104cb0',
        repo: 'Comments',
        owner: 'objcvip',
        admin: "objcvip",
        createIssueManually: false,
        distractionFreeMode: true
    })
    gitalk.render('comment-container')
</script>

    </div>
</div>
</div>
                
                
            </div>
        </div>
    </section>
    <footer class="footer">
    <div class="container">
        <div class="level">
            <div class="level-start has-text-centered-mobile">
                <a class="footer-logo is-block has-mb-6" href="/">
                
                    <img src="/images/logo.svg" alt="iOS 代码规范" height="28">
                
                </a>
                <p class="is-size-7">
                &copy; 2020 ObjC.vip&nbsp;
                All rights reserved.
                
                </p>
            </div>
            <div class="level-end">
            
            </div>
        </div>
    </div>
</footer>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@2.22.2/min/moment-with-locales.min.js"></script>
<script>moment.locale("zh-CN");</script>

<script>
var IcarusThemeSettings = {
    article: {
        highlight: {
            clipboard: true,
            fold: 'unfolded'
        }
    }
};
</script>


    <script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.4/dist/clipboard.min.js" defer></script>



    
    
    
    <script src="/js/animation.js"></script>
    

    
    
    
    <script src="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/js/lightgallery.min.js" defer></script>
    <script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js" defer></script>
    <script src="/js/gallery.js" defer></script>
    

    
    

<div id="outdated">
    <h6>Your browser is out-of-date!</h6>
    <p>Update your browser to view this website correctly. <a id="btnUpdateBrowser" href="http://outdatedbrowser.com/">Update
            my browser now </a></p>
    <p class="last"><a href="#" id="btnCloseUpdateBrowser" title="Close">&times;</a></p>
</div>
<script src="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.js" defer></script>
<script>
    document.addEventListener("DOMContentLoaded", function () {
        outdatedBrowser({
            bgColor: '#f25648',
            color: '#ffffff',
            lowerThan: 'flex'
        });
    });
</script>


    
    
<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.5/unpacked/MathJax.js?config=TeX-MML-AM_CHTML" defer></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
    MathJax.Hub.Config({
        'HTML-CSS': {
            matchFontHeight: false
        },
        SVG: {
            matchFontHeight: false
        },
        CommonHTML: {
            matchFontHeight: false
        },
        tex2jax: {
            inlineMath: [
                ['$','$'],
                ['\\(','\\)']
            ]
        }
    });
});
</script>

    
    

<a id="back-to-top" title="回到顶端" href="javascript:;">
    <i class="fas fa-chevron-up"></i>
</a>
<script src="/js/back-to-top.js" defer></script>


    
    

    
    
    
    

    
    
    
    
    


<script src="/js/main.js" defer></script>

    
    <div class="searchbox ins-search">
    <div class="searchbox-container ins-search-container">
        <div class="searchbox-input-wrapper">
            <input type="text" class="searchbox-input ins-search-input" placeholder="想要查找什么..." />
            <span class="searchbox-close ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="searchbox-result-wrapper ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
    (function (window) {
        var INSIGHT_CONFIG = {
            TRANSLATION: {
                POSTS: '文章',
                PAGES: '页面',
                CATEGORIES: '分类',
                TAGS: '标签',
                UNTITLED: '(无标题)',
            },
            CONTENT_URL: '/content.json',
        };
        window.INSIGHT_CONFIG = INSIGHT_CONFIG;
    })(window);
</script>
<script src="/js/insight.js" defer></script>
<link rel="stylesheet" href="/css/search.css">
<link rel="stylesheet" href="/css/insight.css">
    
</body>
</html>